package com.ujiuye.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.domain.dto.PageDTO;
import com.ujiuye.domain.po.Actor;
import com.ujiuye.domain.po.Region;
import com.ujiuye.domain.dto.SaveActorDTO;
import com.ujiuye.domain.vo.PageVO;
import com.ujiuye.service.IActorService;
import com.ujiuye.service.IRegionService;
import com.ujiuye.utils.FileLoad;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("actor")
@Slf4j
public class ActorController {

    @Autowired//依赖注入
    private IActorService actorService;

    @Autowired
    private IRegionService regionService;

    @Value("${upload.dir}")
    private String uploadDir;

    @PostMapping("/delete")
    public String delete(@RequestBody Actor actor) {
        actorService.delete(actor.getId());
        return "删除成功";
    }

    // 更新演员
    @PostMapping("/update")
    public String update(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam(value = "actor") String actorJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        SaveActorDTO actorDTo = null;
        try {
            actorDTo = objectMapper.readValue(actorJson, SaveActorDTO.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        //参数拷贝
        Actor actor = new Actor();
        BeanUtils.copyProperties(actorDTo, actor);
        //将选择的数据从集合转化为字符串
        ArrayList<Integer> regionsStr = actorDTo.getRegionsStr();
        StringJoiner stringJoiner = new StringJoiner(",");
        regionsStr.forEach(item -> {
            stringJoiner.add(String.valueOf(item));
        });
        actor.setRegionsStr(stringJoiner.toString());
        // 如果照片不为空,就把图片转成base64的字符串设置给actor的pic属性
        if (file != null) {
            try {
                actor.setPic(Base64.encodeBase64String(file.getBytes()));
                // 1.上传照片
                // 上传缩略图
                FileLoad.uploadFile(file, uploadDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 2.更新演员的信息
        actorService.update(actor);

        return "提交成功!!";
    }

    // 编辑演员
    @GetMapping("/editActor")
    public Map<String, Object> editActor(int actorId) {
        // 1.获得演员的信息:actor对象
        Actor actor = actorService.findById(actorId);
        // 2.获得区域列表: regionList
        List<Region> regionList = regionService.findAll();
        // 3.定义一个map用来封装演员和区域列表
        Map<String, Object> map = new HashMap<>();
        map.put("actor", actor);
        map.put("regionList", regionList);
        // 返回map
        return map;
    }


    @GetMapping("/list")
    public PageVO<Actor> list(PageDTO<String> pageDTO) {
        PageVO<Actor> pageVO = actorService.page(pageDTO);
        return pageVO;
    }

    // 查询所有的演员区域: 返回区域列表
    @GetMapping("/findAllRegion")
    public List<Region> findAllRegion() {
        return regionService.findAll();
    }

    // 保存演员
    @PostMapping("/save")
    public String save(@RequestParam(value = "file", required = false) MultipartFile file,
                       @RequestParam(value = "actor") String actorJson) {

        ObjectMapper objectMapper = new ObjectMapper();
        SaveActorDTO actorDTo = null;
        try {
            actorDTo = objectMapper.readValue(actorJson, SaveActorDTO.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        //参数拷贝
        Actor actor = new Actor();
        BeanUtils.copyProperties(actorDTo, actor);
        //将选择的数据从集合转化为字符串
        ArrayList<Integer> regionsStr = actorDTo.getRegionsStr();
        StringJoiner stringJoiner = new StringJoiner(",");
        regionsStr.forEach(item -> {
            stringJoiner.add(String.valueOf(item));
        });
        actor.setRegionsStr(stringJoiner.toString());

        // 如果照片不为空,就把图片转成base64的字符串设置给actor的pic属性
        if (file != null) {
            try {
                actor.setPic(Base64.encodeBase64String(file.getBytes()));
                // 1.上传照片
                // 上传缩略图
                FileLoad.uploadFile(file, uploadDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 2.保存演员的信息
        actorService.save(actor);

        return "提交成功!!";
    }
}
